/*
 * Copyright (c) 2019 Hitevision
 */

package tw.com.hitevision.whiteboard.android.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

import androidx.constraintlayout.widget.ConstraintLayout;
import tw.com.hitevision.whiteboard.android.Props;
import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.Settings;

public class SettingsMenuView extends RelativeLayout {
    private Settings settings;
    private Listener listener;

    public SettingsMenuView(Context context) {
        super(context);
        init(context);
    }

    public SettingsMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SettingsMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @SuppressLint("ClickableViewAccessibility")
    private void init(Context context) {
        settings = Settings.getInstance(context);

        setBackgroundResource(R.drawable.ic_menu_bubble_setting);

        if (Props.getFlavorModel().equals(Props.Flavor.Model.Oem)) {
            setBackgroundTintList(context.getResources().getColorStateList(R.color.oem_theme_color));
        }

        final Resources r = getResources();

        final int paddingTop = r.getDimensionPixelSize(R.dimen.settings_menu_padding_top);
        final int padding = r.getDimensionPixelSize(R.dimen.settings_menu_padding);
        setPadding(padding, paddingTop, padding, 0);

        inflate(context, R.layout.menu_settings, this);

        // implement recognition mode

        final CompoundButton implementRecognition = findViewById(R.id.implement_recognition_option);
        implementRecognition.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                settings.setImplementRecognitionMode(
                        isChecked ? Settings.IMPLEMENT_RECOGNITION_OFF : Settings.IMPLEMENT_RECOGNITION_ALL
                );
            }
        });
        implementRecognition.setChecked(settings.getImplementRecognitionMode() != Settings.IMPLEMENT_RECOGNITION_ALL);

        final View implementRecognitionOn = findViewById(R.id.implement_recognition_on);
        implementRecognitionOn.setOnClickListener(v -> {
            implementRecognition.setChecked(false);
        });

        final View implementRecognitionOff = findViewById(R.id.implement_recognition_off);
        implementRecognitionOff.setOnClickListener(v -> {
            implementRecognition.setChecked(true);
        });

        implementRecognitionOff.setOnTouchListener(new OnTouchListener() {
            private static final long TIME_PERIOD = 2000L; // ms
            private static final int CLICK_COUNT = 2;

            private long start=0;
            private int count;
            private long end = 0;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getActionMasked() != MotionEvent.ACTION_DOWN)
                    return false;
                if (count == 0) {
                    // initial click
                    start = System.currentTimeMillis();
                    count++;
                } else if (System.currentTimeMillis() - start < TIME_PERIOD) {
                    // click is within time period, count it
                    count++;
                } else {
                    // time has expired, start over
                    start = System.currentTimeMillis();
                    count = 1;
                }
                if (count >= CLICK_COUNT && ( Math.abs(start - end) > TIME_PERIOD)) {
                    // toggle build info visibility and reset count
                    count = 0;
                    listener.onMultiTouchStart();
                    end = System.currentTimeMillis();

                }
                return false;
            }
        });

    }

    public void setAnnotationBackground(boolean isAnnotation) {
        if(isAnnotation) {
            setBackgroundResource(R.drawable.menu_bubble_setting);
        } else {
            setBackgroundResource(R.drawable.ic_menu_bubble_setting);
        }
    }

    public void show() {
        setVisibility(View.VISIBLE);
    }

    public void hide() {
        setVisibility(View.GONE);
    }

    public boolean isVisible() {
        return getVisibility() == View.VISIBLE;
    }

    public void setOffsetRight(int offset) {
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) getLayoutParams();
        params.rightMargin = offset;
        setLayoutParams(params);
    }

    public interface Listener {
        void onMultiTouchStart();
    }

}
